<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加系统账号' : '编辑系统账号'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="用户类型：" prop="userType"
              ><el-select v-model="ruleForm.userType" placeholder="请选择用户类型" clearable :disabled="seeType || userType != 0" @change="typeChange">
                <el-option label="超级管理员" value="0"></el-option>
                <el-option label="赛区管理员" value="1"></el-option> </el-select
            ></el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="赛区名称：" prop="matchAreaId">
              <el-select v-model="ruleForm.matchAreaId" placeholder="请选择赛区名称" clearable :disabled="seeType || userType != 0" @change="matchAreaChange">
                <el-option v-for="item in matchAreaArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="角色名称：" prop="roleId">
              <el-select v-model="ruleForm.roleId" placeholder="请选择角色名称" clearable :disabled="seeType">
                <el-option v-for="item in roleArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="账号：" prop="username">
              <el-input v-model="ruleForm.username" placeholder="请输入账号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码：" prop="password">
              <el-input v-model="ruleForm.password" placeholder="请输入密码" show-password clearable maxlength="200" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名：" prop="realName">
              <el-input v-model="ruleForm.realName" placeholder="请输入姓名" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别：" prop="sex">
              <el-select v-model="ruleForm.sex" placeholder="请选择性别" clearable :disabled="seeType">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号：" prop="phone">
              <el-input v-model="ruleForm.phone" placeholder="请输入手机号" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱：" prop="email">
              <el-input v-model="ruleForm.email" placeholder="请输入邮箱" clearable maxlength="100" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">关 闭</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import md5 from "js-md5";
import { getSysUser, saveSysUser, getSysRoles } from "@/api/system";
import { getMatchAreaInfos } from "@/api/matchArea";

export default {
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        userType: "",
        matchAreaId: "",
        roleId: "",
        username: "",
        password: "",
        realName: "",
        sex: "",
        phone: "",
        email: "",
      },
      rules: {
        // 表单规则
        userType: [
          { required: true, message: "请选择用户类型", trigger: "change" },
        ],
        matchAreaId: [
          { required: true, message: "请选择赛区名称", trigger: "change" },
        ],
        roleId: [
          { required: true, message: "请选择角色名称", trigger: "change" },
        ],
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        realName: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        phone: [{ required: false, message: "请输入手机号", trigger: "blur" }],
        email: [{ required: false, message: "请输入邮箱", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      organizationArray: [], // 机构集合
      roleArray: [], // 角色集合
      matchAreaArray: [], // 赛区集合
      oldPassword: "", // 旧密码
      userType: "", // 用户类型（0超级管理员，1赛区管理员）
      matchAreaId: "", // 赛区id
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        this.userType = Number(window.localStorage.getItem("userType"));
        this.matchAreaId = Number(window.localStorage.getItem("matchAreaId"));
        if (this.userType == 1) {
          this.ruleForm.userType = "1";
          this.ruleForm.matchAreaId = this.matchAreaId;

          if (this.addEditStatus == "add") {
            // 获取赛区列表
            this.getMatchAreaInfoList();

            // 获取角色列表
            this.getSysRoleList();
          }
        }

        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getSysUser({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.userType =
            res.data.userType != undefined ? res.data.userType.toString() : "";
          res.data.sex =
            res.data.sex != undefined ? res.data.sex.toString() : "";
          this.oldPassword =
            res.data.password != undefined ? res.data.password : "";
          this.ruleForm = res.data;

          // 获取赛区列表
          this.getMatchAreaInfoList();

          // 获取角色列表
          this.getSysRoleList();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.password != this.oldPassword) {
            this.ruleForm.password = md5(md5(this.ruleForm.password) + "bby");
          }
          saveSysUser(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 用户类型切换
    typeChange() {
      this.ruleForm.matchAreaId = "";
      this.matchAreaArray = [];

      this.ruleForm.roleId = "";
      this.roleArray = [];

      // 获取赛区列表
      this.getMatchAreaInfoList();
    },
    // 获取赛区列表
    getMatchAreaInfoList() {
      if (this.ruleForm.userType == 0) {
        // 管理员，赛区只有一个总部
        this.matchAreaArray = [{ id: 0, name: "总部" }];
      } else {
        getMatchAreaInfos({}).then((res) => {
          if (res.code == 200) {
            this.matchAreaArray = res.data;
          }
        });
      }
    },
    // 赛区下拉切换
    matchAreaChange() {
      this.ruleForm.roleId = "";
      this.roleArray = [];

      // 获取角色列表
      this.getSysRoleList();
    },
    // 获取角色列表
    getSysRoleList() {
      if (this.ruleForm.matchAreaId === "" || this.ruleForm.matchAreaId == null) {
        return;
      }

      getSysRoles({ matchAreaId: this.ruleForm.matchAreaId }).then((res) => {
        if (res.code == 200) {
          this.roleArray = res.data;
          this.matchAreaArray.unshift({ id: 0, name: "总部" });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
